package com.jnks.shardjdbc;

import com.google.common.collect.Range;
import org.apache.shardingsphere.api.sharding.standard.RangeShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.RangeShardingValue;

import java.util.ArrayList;
import java.util.Collection;

public class SubDroneTableTimeRangeAlgorithm implements RangeShardingAlgorithm<String> {

    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<String> shardingValue) {
        Range<String> valueRange = shardingValue.getValueRange();
        //开始时间，范围值的上限
        Integer timeStart = Integer.valueOf(valueRange.lowerEndpoint().toString().substring(0, 7).replace("-", ""));
        //截止时间，范围值的下限
        Integer timeEnd = Integer.valueOf(valueRange.upperEndpoint().toString().substring(0, 7).replace("-", ""));
        Collection<String> list = new ArrayList<>();
        for (String s : availableTargetNames) {
            int startIndex = s.lastIndexOf('_') + 1;
            Integer time = Integer.valueOf(s.substring(startIndex, startIndex + 6));
            if (timeStart <= time && time <= timeEnd) {
                list.add(s);
            }
        }
        return list;

    }
//    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//
//    @Override
//    public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<String> shardingValue) {
//
//        String logicTableName = shardingValue.getLogicTableName();
//        Range<String> valueRange = shardingValue.getValueRange();
//
//        Set<String> queryRangeTables = extracted(logicTableName, LocalDateTime.parse(valueRange.lowerEndpoint(), formatter),
//                LocalDateTime.parse(valueRange.upperEndpoint(), formatter));
//        ArrayList<String> tables = new ArrayList<>(availableTargetNames);
//        tables.retainAll(queryRangeTables);
//        System.out.println(tables);
//        return tables;
//    }
//
//    private Set<String> extracted(String logicTableName, LocalDateTime lowerEndpoint, LocalDateTime upperEndpoint) {
//        Set<String> rangeTable = new HashSet<>();
//        while (lowerEndpoint.isBefore(upperEndpoint)) {
//            String str = getTableNameByDate(lowerEndpoint, logicTableName);
//            rangeTable.add(str);
//            lowerEndpoint = lowerEndpoint.plusMonths(1);
//        }
//
//        //up 月份
//        String str = getTableNameByDate(upperEndpoint, logicTableName);
//        rangeTable.add(str);
//        return rangeTable;
//    }
//
//    private String getTableNameByDate(LocalDateTime dateTime, String logicTableName) {
//        String year = dateTime.getYear() + "";
//        String monthValue = dateTime.getMonthValue() + "";
//        return logicTableName.concat("_").concat(year.concat("_").concat(monthValue));
//    }


//    @Override
//    public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<Date> shardingValue) {
//        Collection<String> result = new LinkedHashSet<>();
//        Range<Date> range = shardingValue.getValueRange();
//        Integer start = toDateMonth(range.lowerEndpoint());
//        int end = toDateMonth(range.upperEndpoint());
//        for (int i = start; i <= end; i = toNextDateMonth(i)) {
//            result.add(ACTUAL_TABLE_PREFIX + i);
//        }
//        return result;
//    }
//
//    public static Integer toDateMonth(Date dateTimeMillSec) {
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
//        return Integer.valueOf(sdf.format(dateTimeMillSec));
//    }
//
//    public static Integer toNextDateMonth(Integer dateMonth) {
//        SimpleDateFormat dft = new SimpleDateFormat("yyyyMM");
//        try {
//            Date date = dft.parse(dateMonth.toString());
//            Calendar cal = Calendar.getInstance();
//            cal.setTime(new Date(date.getTime()));
//            cal.add(Calendar.MONTH, 1);
//            String preMonth = dft.format(cal.getTime());
//            return Integer.valueOf(preMonth);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        return 0;
//    }
}


